<?php
declare(strict_types=1);

namespace app\common\lib;


/**
 * | Notes：海报
 * +----------------------------------------------------------------------
 * | PHP Version 7.2+
 * +----------------------------------------------------------------------
 * | Copyright (c) 2011-2020 https://www.xxq.com.cn, All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 和好 <389123492@qq.com>
 * +----------------------------------------------------------------------
 * | Date: 2020/4/19 19:03
 * +----------------------------------------------------------------------
 */
trait Poster
{
    public static function appPoster(string $background = '', $image, $new_url = '')
    {
        // 资源
        $im = imagecreatefrompng($background);
        // 目录
        $dir_path = root_path();

        // 创建颜色
        // 底色
        $color = imagecolorallocate($im, 255, 254, 253);
        // 淡黑色
        $gray_black = imagecolorallocate($im, 124, 124, 124);

        imagefill($im, 0, 0, $color);

        $font_file_msyhbd = $dir_path . 'public/static/fonts/msyhbd.ttc';
        $font_file_msyh = $dir_path . 'public/static/fonts/msyh.ttc';
        $font_file_apple = $dir_path . 'public/static/fonts/apple.ttf';
        $font_file_jianti = $dir_path . 'public/static/fonts/jianti.ttf';

        // 商品图片
        [$goods_w, $goods_h] = getimagesize($image['qr_url']);
        $goods_res = self::getResourceImg($image['qr_url']);
        imagecopyresized($im, $goods_res, 230, 600, 0, 0, 300, 300, $goods_w, $goods_h);

        //imagettftext($im, 16, 0, 240, 1150, $gray_black, $font_file_msyh, $image['number']);
        ob_clean();
        header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', false);
        header('Pragma: no-cache');
        header("content-type: image/png");

        if (empty($new_url)) {
            imagepng($im);
        } else {
            imagepng($im, $new_url);
            //self::getPrintImg($im, $new_url);
        }

        imagedestroy($im);
    }

    public static function appPosterRanking(string $background = '', $image, $new_url = '')
    {
        // 资源
        $im = imagecreatefromjpeg($background);
        // 目录
        $dir_path = root_path();

        // 创建颜色
        // 底色
        $color = imagecolorallocate($im, 255, 254, 253);
        // 淡黑色
        $gray_black = imagecolorallocate($im, 124, 124, 124);
        // 黑色
        $block_black = imagecolorallocate($im, 2, 0, 65);
        // 蓝色
        $blue_black = imagecolorallocate($im, 52, 62, 183);

        imagefill($im, 0, 0, $color);

        $font_file_msyhbd = $dir_path . 'public/static/fonts/msyhbd.ttc';
        $font_file_msyh = $dir_path . 'public/static/fonts/msyh.ttc';
        $font_file_apple = $dir_path . 'public/static/fonts/apple.ttf';
        $font_file_jianti = $dir_path . 'public/static/fonts/jianti.ttf';

        // 商品图片
        [$goods_w, $goods_h] = getimagesize($image['qr_url']);
        $goods_res = self::getResourceImg($image['qr_url']);
        imagecopyresized($im, $goods_res, 555, 1260, 0, 0, 150, 150, $goods_w, $goods_h);

        imagettftext($im, 30, 0, 225, 770, $color, $font_file_msyhbd, date('Y年m月d日'));
        imagettftext($im, 28, 0, 300, 900, $block_black, $font_file_msyhbd, $image['nickname']);

        $pianyi1 = 0;
        $pianyi2 = 0;
        if ((float)$image['today_liang'] > 100000) {
            $pianyi1 = -50;
        } else if ((float)$image['today_liang'] > 10000) {
            $pianyi1 = -40;
        } else if ((float)$image['today_liang'] > 1000) {
            $pianyi1 = -30;
        } else if ((float)$image['today_liang'] > 100) {
            $pianyi1 = -20;
        } else if ((float)$image['today_liang'] > 10) {
            $pianyi1 = -10;
        }
        if ((float)$image['total_liang'] > 100000) {
            $pianyi2 = -50;
        } else if ((float)$image['total_liang'] > 10000) {
            $pianyi2 = -40;
        } else if ((float)$image['total_liang'] > 1000) {
            $pianyi2 = -30;
        } else if ((float)$image['total_liang'] > 100) {
            $pianyi1 = -20;
        } else if ((float)$image['total_liang'] > 10) {
            $pianyi1 = -10;
        }
        imagettftext($im, 26, 0, 175 + $pianyi1, 1000, $blue_black, $font_file_msyhbd, $image['today_liang']);
        imagettftext($im, 26, 0, 450 + $pianyi2, 1000, $blue_black, $font_file_msyhbd, $image['total_liang']);

        ob_clean();
        header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', false);
        header('Pragma: no-cache');
        header("content-type: image/png");

        if (empty($new_url)) {
            imagejpeg($im);
        } else {
            imagejpeg($im, $new_url);
            //self::getPrintImg($im, $new_url);
        }

        imagedestroy($im);
    }

    /**
     * 根据绝对路径的图片地址获取对应的图片资源
     *
     * @param $img
     *
     * @return false|resource|null
     */
    public static function getResourceImg($img)
    {
        $wh = getimagesize($img);//比pathinfo要准
        $src_img = null;
        switch ($wh[2]) {
            case 1:
                //gif
                $src_img = imagecreatefromgif($img);
                break;
            case 2:
                //jpg
                $src_img = imagecreatefromjpeg($img);
                break;
            case 3:
                //png
                $src_img = imagecreatefrompng($img);
                break;
        }
        return $src_img;
    }

    public static function refereePoster($background = '', $image, $new_url = '')
    {
        // 资源
        $im = imagecreatefromjpeg($background);
        // 目录
        $dir_path = root_path();

        // 创建颜色
        // 底色
        $color = imagecolorallocate($im, 255, 254, 253);
        // 白色
        $white = imagecolorallocate($im, 255, 255, 255);
        // 淡黑色
        $gray_black = imagecolorallocate($im, 124, 124, 124);

        imagefill($im, 0, 0, $color);

        $font_file_msyhbd = $dir_path . 'public/static/fonts/msyhbd.ttc';
        $font_file_msyh = $dir_path . 'public/static/fonts/msyh.ttc';
        $font_file_apple = $dir_path . 'public/static/fonts/apple.ttf';
        $font_file_jianti = $dir_path . 'public/static/fonts/jianti.ttf';

        // 二维码图片
        [$goods_w, $goods_h] = getimagesize($image['qr_url']);
        $goods_res = self::getResourceImg($image['qr_url']);
        imagecopyresized($im, $goods_res, 262, 940, 0, 0, 195, 195, $goods_w, $goods_h);

        // 个人头像图片
        [$goods_w, $goods_h] = getimagesize($image['avatar_url']);
        $goods_res = self::getResourceImg($image['avatar_url']);
        imagecopyresized($im, $goods_res, 171, 1184, 0, 0, 74, 74, $goods_w, $goods_h);

        imagettftext($im, 18, 0, 265, 1209, $white, $font_file_msyh, '你的好友"' . $image['nickname'] . '"邀请您加入喜赠');
        imagettftext($im, 16, 0, 265, 1244, $white, $font_file_msyh, $image['number']);
        ob_clean();
        header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', false);
        header('Pragma: no-cache');
        header("content-type: image/png");

        if (empty($new_url)) {
            imagejpeg($im);
        } else {
            imagejpeg($im, $new_url);
            //self::getPrintImg($im, $new_url);
        }

        imagedestroy($im);
    }
}